//
//  ChooseCityViewController.m
//  GetPowerDBO
//
//  Created by 周博 on 2017/6/21.
//  Copyright © 2017年 BogoZhou. All rights reserved.
//

#import "ChooseCityViewController.h"

@interface ChooseCityViewController ()<UIPickerViewDelegate,UIPickerViewDataSource>
{
    NSString *_chooseItem;
    NSString *_chooseId;
}

@property (nonatomic,strong) NSArray *dataArray;

@property (nonatomic,strong) NSMutableDictionary *allDictionary;

@property (nonatomic,strong) NSMutableArray *provinceArray;

@property (nonatomic,strong) UIPickerView *myPicker;

@property (nonatomic,strong) NSArray *areas;

@property (nonatomic,strong) NSString *selectAreas;

@property (nonatomic,strong) NSString *selectAreas2;

@property (nonatomic,strong) NSMutableDictionary *countyDic;

@property (nonatomic,strong) NSString *pId;

@property (nonatomic,strong) NSString *cId;

@property (nonatomic,strong) NSString *aId;

@property (nonatomic,strong) NSString *pName;

@property (nonatomic,strong) NSString *cName;

@property (nonatomic,strong) NSString *aName;

@end

@implementation ChooseCityViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    _dataArray = [self getAllInfo];
    //    NSLog(@"%@",_dataArray);
    [self creatMyPicker];
}

- (void)creatMyPicker{
    
    UIButton *backButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    
    [backButton setBackgroundColor:[UIColor blackColor]];
    backButton.alpha = 0.3;
    [backButton addTarget:self action:@selector(backButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:backButton];
    
    UIView *bigView = [[UIView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height - 250, self.view.frame.size.width, 250)];
    bigView.backgroundColor = [UIColor whiteColor];
    
    [self.view addSubview:bigView];
    
    
    
    
    
    UIView *topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, bigView.frame.size.width, 50)];
    topView.backgroundColor = kColorFrom0x(0xf5f5f5);
    
    
    
    UIButton *cancelButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 80, topView.sizeHeight)];
    [cancelButton setTitle:@"取消" forState:UIControlStateNormal];
    [cancelButton setTitleColor:k9f9f9f forState:UIControlStateNormal];
    [cancelButton addTarget:self action:@selector(backButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *sureButton = [[UIButton alloc] initWithFrame:CGRectMake(topView.sizeWidth - 80, 0, 80, topView.sizeHeight)];
    [sureButton setTitle:@"确定" forState:UIControlStateNormal];
    [sureButton setTitleColor:kGreenColor forState:UIControlStateNormal];
    [sureButton addTarget:self action:@selector(sureButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    
    [topView addSubview:cancelButton];
    [topView addSubview:sureButton];
    
    UIImageView *lineImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, topView.sizeHeight-1, topView.sizeWidth, 1)];
    lineImageView.backgroundColor = kd6d6d6;
    [topView addSubview:lineImageView];
    [bigView addSubview:topView];
    
    
    _myPicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height - 200, self.view.frame.size.width, 200)];
    
    _myPicker.delegate = self;
    _myPicker.dataSource = self;
    
    [self.view addSubview:_myPicker];
    
    _chooseItem = @"辽宁省-葫芦岛市-兴城市";
    _chooseId = @"6-3020-3026";
    
    _pName = @"辽宁省";
    _cName = @"葫芦岛市";
    _aName = @"兴城市";
    
    _pId = @"6";
    _cId = @"3020";
    _aId = @"3026";
    
}

- (void)backButtonClick:(UIButton *)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}


- (void)sureButtonClick:(UIButton *)button{
//    NSDictionary *dict = @{
//                           @"indexPath" : _indexPath,
//                           @"name" : _chooseItem,
//                           @"id" : _chooseId
//                           };
//    [[NSNotificationCenter defaultCenter] postNotificationName:@"place" object:dict];
    [_delegate getShopProvinceName:_pName pId:_pId cityName:_cName cityId:_cId areaName:_aName areaId:_aId];

    [self backButtonClick:nil];
}

- (NSArray *)getAllInfo{
    //解析数据
    
    _allDictionary = [NSMutableDictionary dictionary];
    _countyDic = [NSMutableDictionary dictionary];
    
    _provinceArray = [NSMutableArray array];
    
    NSString *filePath = [[NSBundle mainBundle]pathForResource:@"address" ofType:@"json"];
    
    NSData *data= [NSData dataWithContentsOfFile:filePath];
    
    NSDictionary *allDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
    
    NSArray *array = allDic[@"data"];
    
    //遍历获取所有省
    for (NSDictionary *dic in array) {
        if ([dic[@"pid"] integerValue] == 0) {
            [_provinceArray addObject:dic];
        }
    }
    
    //遍历获取所有省对应的市
    for (NSDictionary *dic in _provinceArray) {
        NSMutableArray *citiesAr = [NSMutableArray array];
        for (NSMutableDictionary *dic2 in array) {
            NSMutableArray *countyAr = [NSMutableArray array];
            if ([dic[@"id"] isEqualToString:dic2[@"pid"]]) {
                
                
                for (NSDictionary *dic3 in array) {
                    if ([dic2[@"id"] isEqualToString:dic3[@"pid"]]) {
                        [countyAr addObject:dic3];
                    }
                    
                }
                NSMutableDictionary *copyDic2 = [NSMutableDictionary dictionaryWithDictionary:dic2];
                [_countyDic setObject:countyAr forKey:dic2[@"name"]];
                [citiesAr addObject:copyDic2];
                
            }
        }
        
        
        [_allDictionary setObject:citiesAr forKey:dic[@"name"]];
    }
//    NSDictionary *pDic = _provinceArray[0];
//    _pId =pDic[@"id"];
    _areas = [_allDictionary allKeys];
    
    _selectAreas = _areas[0];
    
    
    NSArray *ar = [_allDictionary valueForKey:_selectAreas];
    NSDictionary *d = ar[0];
    _selectAreas2 = d[@"name"];
    
    return array;
}

#pragma mark - pickerViewDelegate

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 3;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    if (component == 0) {
        return _provinceArray.count;
    }else if(component == 1){
        return [[_allDictionary objectForKey:_selectAreas] count];
    }else{
        return [[_countyDic objectForKey:_selectAreas2] count];
    }
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    NSDictionary *dict;
    
    if (component == 0) {
        return _areas[row];
    }else if (component == 1){
        dict= [[_allDictionary objectForKey:_selectAreas] objectAtIndex:row];
        return dict[@"name"];
    }else{
        dict= [[_countyDic objectForKey:_selectAreas2] objectAtIndex:row];
        return dict[@"name"];
    }
    
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    NSInteger myRow = 0;

    if (component == 0) {
        _selectAreas = [_areas objectAtIndex:row];
        [self.myPicker reloadComponent:1];
        //        [self.myPicker reloadAllComponents];
        NSDictionary * dict= [[_allDictionary objectForKey:_selectAreas] objectAtIndex:0];
        _selectAreas2 =dict[@"name"];
        _cId = dict[@"id"];
        _pId = dict[@"pid"];
        [self.myPicker reloadComponent:2];
    }else if (component == 1){
        NSDictionary * dict= [[_allDictionary objectForKey:_selectAreas] objectAtIndex:row];
        _selectAreas2 =dict[@"name"];
        _cId = dict[@"id"];
        _pId = dict[@"pid"];
        [self.myPicker reloadComponent:2];
    }else{
        myRow = row;
    }
    NSDictionary *dict= [[_countyDic objectForKey:_selectAreas2] objectAtIndex:myRow];
    _aId = dict[@"id"];
    _chooseItem = [NSString stringWithFormat:@"%@-%@-%@",_selectAreas,_selectAreas2,dict[@"name"]];
    _chooseId = [NSString stringWithFormat:@"%@-%@-%@",_pId,_cId,_aId];
    
    NSLog(@"%@,%@",_chooseItem,_chooseId);
    
    _pName = _selectAreas;
    _cName = _selectAreas2;
    _aName = dict[@"name"];
}


//重写方法
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
    UILabel* pickerLabel = (UILabel*)view;
    if (!pickerLabel){
        pickerLabel = [[UILabel alloc] init];
        // Setup label properties - frame, font, colors etc
        //adjustsFontSizeToFitWidth property to YES
        pickerLabel.adjustsFontSizeToFitWidth = YES;
        [pickerLabel setTextAlignment:NSTextAlignmentCenter];
        [pickerLabel setBackgroundColor:[UIColor clearColor]];
        [pickerLabel setFont:[UIFont boldSystemFontOfSize:15]];
    }
    // Fill the label text here
    pickerLabel.text=[self pickerView:pickerView titleForRow:row forComponent:component];
    return pickerLabel;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end
